//
//  DHFilterViewController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/9.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHFilterViewController.h"
#import "DHPriceSceCell.h"
#import "DHChectOutCell.h"
#import "DHClearAreaCell.h"
#import "DHFilterFootView.h"
#import "DHAreaCell.h"
#import <CZPickerView.h>
#import "DHAreaModel.h"
NSString *priceSceID = @"priceSceID";
NSString *checkOutID = @"checkOutID";
NSString *areaCellID = @"areaCellID";
NSString *clearAreaID = @"clearAreaID";
@interface DHFilterViewController ()<UITableViewDataSource,
                                    UITableViewDelegate,
                                    DHAreaCellDelegate,
                                    CZPickerViewDataSource,
                                    CZPickerViewDelegate,DHPriceSceCellDelegate,
                                    DHFilterFootViewDelegate, DHChectOutCellDelegate>

@property(nonatomic ,strong)UITableView *filterView;
@property(nonatomic ,strong)UIButton *leftNavItem;
@property(nonatomic ,strong)CZPickerView *pickerView;

@property(nonatomic ,strong)DHFilterViewModel *viewModel;

@property(nonatomic ,strong)DHAreaModel *province;

@property(nonatomic ,strong)NSString *lowPrice; // 最低价钱
@property(nonatomic ,strong)NSString *highPrice; //最高价钱

@property(nonatomic ,strong)NSString *isCheckout; // 是否退房(1-否, 2-是)

//@property(nonatomic ,strong)NSMutableArray *clearList; //清洁区域id数组

@property(nonatomic ,strong)NSString *clearID; // 清洁区域id
@end

@implementation DHFilterViewController
#pragma mark: - 懒加载viewModel
- (DHFilterViewModel *)viewModel
{
    if (!_viewModel)
    {
        _viewModel = [[DHFilterViewModel alloc] init];
    [self loadAreaData];
    }
    return _viewModel;
}
#pragma mark: - 左边导航栏按钮懒加载
- (UIButton *)leftNavItem
{
    if (!_leftNavItem)
    {
        _leftNavItem = [[UIButton alloc] init];
        UIImage *img = [UIImage createNewImage:[UIImage imageNamed:@"back"] newSize:CGSizeMake(22, 22)];
        [_leftNavItem setImage:img forState:UIControlStateNormal];
        [_leftNavItem addTarget:self action:@selector(popButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftNavItem;
}
#pragma mark: - 左边导航栏按钮监听
- (void)popButtonClick
{
    //[self.navigationController popViewControllerAnimated:true];
    [self dismissViewControllerAnimated:true completion:nil];
}


- (UITableView *)filterView
{
    if (!_filterView) {
        _filterView = [[UITableView alloc] initWithFrame:CGRectMake(0,
                    CGRectGetMaxY(self.navigationController.navigationBar.frame),
                                                kScreenWidth * 0.75,
                                            kScreenHeight)];
        _filterView.dataSource = self;
        _filterView.delegate = self;
    }
    return _filterView;
}

- (void)setClearAreaList:(NSArray<DHClearArea *> *)clearAreaList
{
    _clearAreaList = clearAreaList;
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupFilterView];
    [self setupNav];
    [self loadAreaData];
    
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(clearAreaButtonClick:)
                                               name:kClearAreaNotification
                                             object:nil];
}
- (void)dealloc
{
    [NSNotificationCenter.defaultCenter removeObserver:self];
}
#pragma mark: - 清洁区域按钮监听
- (void)clearAreaButtonClick:(NSNotification *)n
{
    self.clearID = n.userInfo[@"clearAreaID"];
}
#pragma mark: - 加载地区数据
- (void)loadAreaData
{
    [self.viewModel loadAreaDataWithWithAreaModel:self.province Completion:^(BOOL isSuccess, NSString *statusInfo)
    {
        if (!isSuccess)
        {
            [SVProgressHUD showErrorWithStatus:@"网络有问题"];
            return ;
        }
        if ([statusInfo isEqualToString:@"服务器有问题"]) {
             [SVProgressHUD showErrorWithStatus:statusInfo];
            return ;
        }
        [self.pickerView reloadData];
        [self.filterView reloadData];
    }];
}
#pragma mark: - 设置导航栏
- (void)setupNav
{
    self.view.backgroundColor = UIColor.whiteColor;
    self.title = @"筛选";
}
#pragma mark: - 设置filterView
- (void)setupFilterView
{
    [self.view addSubview:self.filterView];
    // self.filterView.backgroundColor = UIColor.redColor;
    //[self.filterView registerClass:[DHPriceSceCell class] forCellReuseIdentifier:priceSceID];
    [self.filterView registerNib:[UINib nibWithNibName:@"DHPriceSceCell" bundle:nil] forCellReuseIdentifier:priceSceID];
    [self.filterView registerNib:[UINib nibWithNibName:@"DHChectOutCell" bundle:nil] forCellReuseIdentifier:checkOutID];
    [self.filterView registerNib:[UINib nibWithNibName:@"DHAreaCell" bundle:nil] forCellReuseIdentifier:areaCellID];
    [self.filterView registerClass:[DHClearAreaCell class] forCellReuseIdentifier:clearAreaID];
    //[self.filterView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
    // self.filterView.contentInset = UIEdgeInsetsMake(24, 0, 0, 0 );
    //self.filterView.rowHeight = 84;
    self.filterView.estimatedRowHeight = 150;//估算高度
    self.filterView.rowHeight = UITableViewAutomaticDimension;
    DHFilterFootView *filterView = [DHFilterFootView footViewWithFrame:CGRectMake(0, 0,
                                                                                   kScreenWidth,
                                                                                  55)];
    self.filterView.tableFooterView = filterView;
    filterView.delegate = self;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftNavItem];
    self.filterView.scrollEnabled = false;
    self.filterView.sectionHeaderHeight = 44;
    [self.filterView layoutIfNeeded];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.filterView resignFirstResponder];
}

#pragma mark: - DHPriceSceCellDelegate
- (void)priceSceCellLowPrice:(DHPriceSceCell *)cell lowPrice:(NSString *)lowPrice
{
    self.lowPrice = lowPrice;
}
- (void)priceSceCellHighPrice:(DHPriceSceCell *)cell highPrice:(NSString *)highPrice
{
    self.highPrice = highPrice;
}


#pragma mark: - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getCellReuseIDWithSection:indexPath.section] forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    switch (indexPath.section) {
        case 0:
            [(DHPriceSceCell *)cell setDelegate:self];
            break;
        case 1:
            [(DHChectOutCell *)cell setDelegete:self];
            break;
        case 2:
            [(DHAreaCell *)cell setDelegate:self];
            break;
        case 3:
            [(DHClearAreaCell *)cell setClearAreaList:self.clearAreaList];
            break;
            
        default:
            break;
    }
    return cell;
}

#pragma mark: - DHChectOutCellDelegate
- (void)checkoutCellIsCheckout:(DHChectOutCell *)cell isCheckOut:(NSString *)isCheckOut
{
    self.isCheckout = isCheckOut;
}
#pragma mark: - 筛选订单按钮通知监听
- (void)filterViewDidClickFilterButton:(UIView *)view
{
    NSAssert(self.filterButtonClick != nil, @"self.filterButtonClick 不能为nil");
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if (self.lowPrice) {[params setObject:self.lowPrice forKey:@"min_price"];}
    if (self.highPrice) {[params setObject:self.highPrice forKey:@"max_price"];}
    switch (self.isCheckout.intValue)
    {
        case 1:
            [params setObject:self.isCheckout forKey:@"is_leave"];
            break;
        case 2:
            [params setObject:self.isCheckout forKey:@"is_leave"];
            break;
        default:
            break;
    }
    if (self.province) {[params setObject:self.province.area_id forKey:@"area_id"];}
    if (self.clearID) {[params setObject:self.clearID forKey:@"clear_arae"];}
    
    self.filterButtonClick(params);
}

#pragma mark: - CZPickerViewDataSource
- (NSInteger)numberOfRowsInPickerView:(CZPickerView *)pickerView
{
    return self.viewModel.areaList.list.count;
}
- (NSAttributedString *)czpickerView:(CZPickerView *)pickerView
               attributedTitleForRow:(NSInteger)row
{
    return [[NSAttributedString alloc] initWithString:[self.viewModel.areaList.list[row] area_name]];
}
- (void)czpickerView:(CZPickerView *)pickerView didConfirmWithItemAtRow:(NSInteger)row
{
    self.province = self.viewModel.areaList.list[row];
    DHAreaCell *cell = [self.filterView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];
    switch (self.viewModel.areaList.deep.intValue)
    {
        case 1:
            //省份
            cell.province = [self.viewModel.areaList.list[row] area_name];
            break;
        case 2:
            //城市
            cell.city = [self.viewModel.areaList.list[row] area_name];
            break;
        default:
            break;
    }
    self.viewModel = nil;

}

#pragma mark: - DHAreaCellDelegate
- (void)areaCellSelectedCity:(DHAreaCell *)cell
{
    if (!self.province) {
        [SVProgressHUD showInfoWithStatus:@"请先选择省份"];
        return;
    }
    self.pickerView = [[CZPickerView alloc] initWithHeaderTitle:@"城市"
                                              cancelButtonTitle:@"取消"
                                             confirmButtonTitle:@"确认"];
    
    self.pickerView.needFooterView = true;
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.pickerView show];
}

- (void)areaCellSelectedProvince:(DHAreaCell *)cell
{
 
    self.pickerView = [[CZPickerView alloc] initWithHeaderTitle:@"省份"
                                              cancelButtonTitle:@"取消"
                                             confirmButtonTitle:@"确认"];
    
    self.pickerView.needFooterView = true;
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.pickerView show];

}

#pragma mark: - UITableViewDelegate
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self creatHeaderViewWithSection:section];
}
#pragma mark: - 获取可重用id
- (NSString *)getCellReuseIDWithSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return priceSceID;
            break;
        case 1:
            return checkOutID;
            break;
        case 2:
            return areaCellID;
            break;
        default:
            return clearAreaID;
            break;
    }
}
#pragma mark: - 创建headerView
- (UIView *)creatHeaderViewWithSection:(NSInteger)section
{
    NSString *headerTitle;
    switch (section)
    {
        case 0:
            headerTitle = @"    价格区间";
            break;
        case 1:
            headerTitle = @"    是否退房";
            break;
        case 2:
            headerTitle = @"    任务地区";
            break;
        case 3:
            headerTitle = @"    清洁区域";
            break;
    }
   // NSLog(@"%@",headerTitle);
    UILabel *label = [[UILabel alloc] init];
    label.text = headerTitle;
    label.font = [UIFont systemFontOfSize:12];
    return label;
}

@end
